{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "improving-starter",
   "metadata": {},
   "source": [
    "# Malaysian National Registration Identity Card Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "behavioral-throw",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "impaired-announcement",
   "metadata": {},
   "source": [
    "The function `clean_my_nric()` cleans a column containing Malaysian National Registration Identity Card Number (NRIC) strings, and standardizes them in a given format. The function `validate_my_nric()` validates either a single NRIC strings, a column of NRIC strings or a DataFrame of NRIC strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cathedral-haiti",
   "metadata": {},
   "source": [
    "NRIC strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"770305021234\"\n",
    "* `standard`: NRIC strings with proper whitespace in the proper places, like \"770305-02-1234\"\n",
    "* `birthdate`: return the registration date or the birth date, like \"1977-03-05\".\n",
    "* `birthplace`: return a dict containing the birthplace of the person, like {'state': 'Kedah', 'country': 'Malaysia', 'countries': 'Malaysia'}.\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_my_nric()` and `validate_my_nric()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "backed-delay",
   "metadata": {},
   "source": [
    "### An example dataset containing NRIC strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "studied-burning",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"nric\": [\n",
    "            \"770305021234\",\n",
    "            \"771305-02-1234\",\n",
    "            \"999 999 999\",\n",
    "            \"004085616\",\n",
    "            \"002 724 334\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "protective-partnership",
   "metadata": {},
   "source": [
    "## 1. Default `clean_my_nric`\n",
    "\n",
    "By default, `clean_my_nric` will clean nric strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "boxed-quest",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_my_nric\n",
    "clean_my_nric(df, column = \"nric\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "nuclear-literature",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "toxic-archives",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deluxe-charm",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "under-stereo",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_my_nric(df, column = \"nric\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "widespread-bruce",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "excellent-riverside",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_my_nric(df, column = \"nric\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "noted-reasoning",
   "metadata": {},
   "source": [
    "### `birthdate`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "latter-surge",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_my_nric(df, column = \"nric\", output_format=\"birthdate\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "twenty-county",
   "metadata": {},
   "source": [
    "### `birthplace`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "occupied-month",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_my_nric(df, column = \"nric\", output_format=\"birthplace\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "selected-second",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned NRIC strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "executive-substitute",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_my_nric(df, column=\"nric\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "naked-department",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "terminal-surgery",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "centered-membership",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_my_nric(df, \"nric\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "subsequent-astrology",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fifth-omega",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_my_nric(df, \"nric\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "legal-working",
   "metadata": {},
   "source": [
    "## 4. `validate_my_nric()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "steady-harrison",
   "metadata": {},
   "source": [
    "`validate_my_nric()` returns `True` when the input is a valid NRIC. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_my_nric()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_my_nric()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_my_nric()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "underlying-little",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_my_nric\n",
    "print(validate_my_nric(\"770305021234\"))\n",
    "print(validate_my_nric(\"771305-02-1234\"))\n",
    "print(validate_my_nric(\"999 999 999\"))\n",
    "print(validate_my_nric(\"51824753556\"))\n",
    "print(validate_my_nric(\"004085616\"))\n",
    "print(validate_my_nric(\"hello\"))\n",
    "print(validate_my_nric(np.nan))\n",
    "print(validate_my_nric(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "productive-intervention",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "local-venture",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_my_nric(df[\"nric\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "subsequent-compatibility",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "vital-picture",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_my_nric(df, column=\"nric\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "flying-intent",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "narrow-ethiopia",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_my_nric(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accredited-seeking",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
